﻿using System;
using System.ComponentModel;
using ApplicationGenerator.Versions.BusinessLogic;

namespace ApplicationGenerator.Versions.Forms
{
    /// <summary>
    /// Konwerter wersji. Służy do wyświetlania informacji na property gridzie
    /// </summary>
    public class VersionConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                if (value == null)
                {
                    return string.Empty;
                }
                VersionInfo versionInfo = value as VersionInfo;
                if (versionInfo == null)
                {
                    return string.Empty;
                }

                return versionInfo.Name;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}